﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Collections;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class MP_PositionCommandDtls : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IPositionCommandDtlsBLL CmdPosDtls = new PositionCommandDtlsBLL();
        private IEmployeeTypeBLL emptypeBLL = new EmployeeTypeBLL();
        private IPositionBLL posBLL = new PositionBLL();
        private IPositionTypeBLL postypeBLL = new PositionTypeBLL();
        private IFieldBLL fieldBLL = new FieldBLL();
        private IExecutiveBLL execBLL = new ExecutiveBLL();
        private IClassSpecificationBLL specBLL = new ClassSpecificationBLL();
        private IJobSiteBLL jobsiteBLL = new JobSiteBLL();

        //private PositionCommandDtlsInfo dtlsInfo;
        //private PositionInfo posInfo = new PositionInfo();
        private object _dataItem = null;        

        protected void Page_Load(object sender, EventArgs e)
        {                     

            if (DataItem != null)
            {
                int EmpTypeSeq = Int32.Parse(Session["EMPTYPE"].ToString());

                FillDataToPositionType(EmpTypeSeq);
                FillDataToExecPositions();
                FillDataToJobSite();
                FillDataToPos(EmpTypeSeq); 

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblCmdPosDtlsSeq.Text = DataBinder.Eval(DataItem, "cmd_pos_dtls_seq").ToString();
                    //dtlsInfo = CmdPosDtls.GetPositionCommandDtlsInfo(lblCmdPosDtlsSeq.Text.Length > 0 ? Int32.Parse(lblCmdPosDtlsSeq.Text ) : (int?)null);

                    cmbPosSeq.SelectedValue = DataBinder.Eval(DataItem, "pos_seq").ToString(); // dtlsInfo.PosSeq.ToString();
                    txtOldPosRef.Text = DataBinder.Eval(DataItem, "old_pos_ref").ToString(); //dtlsInfo.OldPosRef;
                    txtNewPosRef.Text = DataBinder.Eval(DataItem, "new_pos_ref").ToString(); //dtlsInfo.NewPosRef;
                    txtOldPosName.Text = DataBinder.Eval(DataItem, "old_pos_name").ToString(); //dtlsInfo.OldPosName;
                    txtNewPosName.Text = DataBinder.Eval(DataItem, "new_pos_name").ToString(); //dtlsInfo.NewPosName;
                    txtDateStart.SetValue(DataBinder.Eval(DataItem, "start_date"));

                    cmbOldPosType.SelectedValue = DataBinder.Eval(DataItem, "old_pos_type_seq").ToString();
                    cmbNewPosType.SelectedValue = DataBinder.Eval(DataItem, "new_pos_type_seq").ToString();

                    FillDataToFieldPositions(cmbOldPosType.SelectedValue, cmbOldFieldSeq);
                    FillDataToFieldPositions(cmbNewPosType.SelectedValue, cmbNewFieldSeq);

                    cmbOldFieldSeq.SelectedValue = DataBinder.Eval(DataItem, "old_field_seq").ToString();
                    cmbNewFieldSeq.SelectedValue = DataBinder.Eval(DataItem, "new_field_seq").ToString();

                    cmbOldExecSeq.SelectedValue = DataBinder.Eval(DataItem, "old_field_seq").ToString();
                    cmbNewExecSeq.SelectedValue = DataBinder.Eval(DataItem, "new_field_seq").ToString();

                    FillDataToClassSpec(cmbOldPosType.SelectedValue, cmbOldClassSpec);
                    FillDataToClassSpec(cmbNewPosType.SelectedValue, cmbNewClassSpec);

                    if (!string.IsNullOrEmpty(DataBinder.Eval(DataItem, "old_class_spec_seq").ToString()))
                        cmbOldClassSpec.SelectedValue = DataBinder.Eval(DataItem, "old_class_spec_seq").ToString();

                    if (!string.IsNullOrEmpty(DataBinder.Eval(DataItem, "new_class_spec_seq").ToString()))
                    cmbNewClassSpec.SelectedValue = DataBinder.Eval(DataItem, "new_class_spec_seq").ToString();

                    cmbOldJobSiteSeq.SelectedValue = DataBinder.Eval(DataItem, "old_job_site_seq").ToString();
                    cmbNewJobSiteSeq.SelectedValue = DataBinder.Eval(DataItem, "new_job_site_seq").ToString();

                    txtOldSalary.Text = DataBinder.Eval(DataItem, "old_salary_quota").ToString(); //dtlsInfo.OldSalaryQuota.ToString();
                    txtNewSalary.Text = DataBinder.Eval(DataItem, "new_salary_quota").ToString(); //dtlsInfo.NewSalaryQuota.ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "remarks").ToString(); //dtlsInfo.Remarks;
                }
                else
                {

                }
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        public void FillDataToPos(int nEmpTypeSeq)
        {
            DataSet ds = posBLL.LoadPositions((int?)null, nEmpTypeSeq, (int?)null, "");
            DataTable dt = null;
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                cmbPosSeq.DataSource = dt;
                cmbPosSeq.DataBind();
            }
        }

        public void FillDataToPositionType(Nullable<int> nEmpType)
        {
            IList<PositionTypeInfo> InfoList = postypeBLL.ListPositionTypes(null, nEmpType);

            cmbOldPosType.DataSource = InfoList;
            cmbOldPosType.DataBind();
            cmbNewPosType.DataSource = InfoList;
            cmbNewPosType.DataBind();
        }

        public void FillDataToFieldPositions(string PosTypeSeq, RadComboBox cmbFieldSeq)
        {
            IList<FieldInfo> InfoList = fieldBLL.ListFields(PosTypeSeq, "");

            cmbFieldSeq.DataSource = InfoList;
            cmbFieldSeq.DataBind();
        }

        public void FillDataToClassSpec(string PosType , RadComboBox cmbClassSpec)
        {
            Nullable<int> nPosTypeSeq = string.IsNullOrEmpty(PosType) ? (int?)null : Int32.Parse(PosType);

            DataSet ds = specBLL.LoadClassSpecification((int?)null, nPosTypeSeq);
            DataTable dt = null;
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                cmbClassSpec.DataSource = dt;
                cmbClassSpec.DataBind();
            }
        }
        public void FillDataToExecPositions()
        {
            IList<ExecutiveInfo> InfoList = execBLL.ListExecutives(null, SystemInfo._ActiveRecordStatus);

            cmbOldExecSeq.DataSource = InfoList;
            cmbOldExecSeq.DataBind();
            cmbNewExecSeq.DataSource = InfoList;
            cmbNewExecSeq.DataBind();
        }

        protected void FillDataToJobSite()
        {
            JobSiteTypeInfo Info = new JobSiteTypeInfo();
            IList<JobSiteInfo> InfoList = jobsiteBLL.ListJobSiteByTypes(SystemInfo._JobsiteExternal);
            
            cmbOldJobSiteSeq.DataSource = InfoList;
            cmbOldJobSiteSeq.DataBind();
            cmbNewJobSiteSeq.DataSource = InfoList;
            cmbNewJobSiteSeq.DataBind();
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            PositionCommandDtlsInfo Info = new PositionCommandDtlsInfo();

            Info.CmdPosSeq = Request.QueryString["CmdPosSeq"].ToString().Length > 0 ? Int32.Parse(Request.QueryString["CmdPosSeq"].ToString()) : (int?)null;
            Info.PosSeq = cmbPosSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbPosSeq.SelectedValue) : (int?)null;
            Info.OldPosRef = txtOldPosRef.Text.Length > 0 ? txtOldPosRef.Text : "";
            Info.NewPosRef = txtNewPosRef.Text.Length > 0 ? txtNewPosRef.Text : "";
            Info.OldPosName = txtOldPosName.Text.Length > 0 ? txtOldPosName.Text : "";
            Info.NewPosName = txtNewPosName.Text.Length > 0 ? txtNewPosName.Text : "";
            Info.OldJobSiteSeq = cmbOldJobSiteSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbOldJobSiteSeq.SelectedValue) : (int?)null;
            Info.NewJobSiteSeq = cmbNewJobSiteSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbNewJobSiteSeq.SelectedValue) : (int?)null;
            Info.OldJobSiteName = cmbOldJobSiteSeq.SelectedValue.Length > 0 ? cmbOldJobSiteSeq.SelectedItem.Text : "";
            Info.NewJobSiteName = cmbNewJobSiteSeq.SelectedValue.Length > 0 ? cmbNewJobSiteSeq.SelectedItem.Text : "";
            Info.OldFieldSeq = cmbOldFieldSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbOldFieldSeq.SelectedValue) : (int?)null;
            Info.NewFieldSeq = cmbNewFieldSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbNewFieldSeq.SelectedValue) : (int?)null;
            Info.OldExecSeq = cmbOldExecSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbOldExecSeq.SelectedValue) : (int?)null;
            Info.NewExecSeq = cmbNewExecSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbNewExecSeq.SelectedValue) : (int?)null;
            Info.OldPosTypeSeq = cmbOldPosType.SelectedValue.Length > 0 ? Int32.Parse(cmbOldPosType.SelectedValue) : (int?)null;
            Info.NewPosTypeSeq = cmbNewPosType.SelectedValue.Length > 0 ? Int32.Parse(cmbNewPosType.SelectedValue) : (int?)null;
            Info.OldClassSpecSeq = cmbOldClassSpec.SelectedValue.Length > 0 ? Int32.Parse(cmbOldClassSpec.SelectedValue) : (int?)null;
            Info.NewClassSpecSeq = cmbNewClassSpec.SelectedValue.Length > 0 ? Int32.Parse(cmbNewClassSpec.SelectedValue) : (int?)null;
            Info.OldSalaryQuota = txtOldSalary.Text.Length > 0 ? double.Parse(txtOldSalary.Text) : (double?)null;
            Info.NewSalaryQuota = txtNewSalary.Text.Length > 0 ? double.Parse(txtNewSalary.Text) : (double?)null;
            Info.StartDate = txtDateStart.GetStringValue().Length > 0 ? DateTime.Parse(txtDateStart.GetStringValue()) : (DateTime?)null;
            Info.Remarks = txtRemarks.Text.Length > 0 ? txtRemarks.Text : "";
            Info.CreatedBy = ssMng.EmpSeqI;
            
            CmdPosDtls.Insert(Info);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //I_HRM_MP_PositionCommandDtls CmdPosDtls = new B_HRM_MP_PositionCommandDtls();
            PositionCommandDtlsInfo Info = new PositionCommandDtlsInfo();

            Info.Seq = lblCmdPosDtlsSeq.Text.Length > 0 ? Int32.Parse(lblCmdPosDtlsSeq.Text) : (int?)null;
            Info.CmdPosSeq = Request.QueryString["CmdPosSeq"].ToString().Length > 0 ? Int32.Parse(Request.QueryString["CmdPosSeq"].ToString()) : (int?)null;
            Info.PosSeq = cmbPosSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbPosSeq.SelectedValue) : (int?)null;
            Info.OldPosRef = txtOldPosRef.Text.Length > 0 ? txtOldPosRef.Text : "";
            Info.NewPosRef = txtNewPosRef.Text.Length > 0 ? txtNewPosRef.Text : "";
            Info.OldPosName = txtOldPosName.Text.Length > 0 ? txtOldPosName.Text : "";
            Info.NewPosName = txtNewPosName.Text.Length > 0 ? txtNewPosName.Text : "";
            Info.OldJobSiteSeq = cmbOldJobSiteSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbOldJobSiteSeq.SelectedValue) : (int?)null;
            Info.NewJobSiteSeq = cmbNewJobSiteSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbNewJobSiteSeq.SelectedValue) : (int?)null;
            Info.OldJobSiteName = cmbOldJobSiteSeq.SelectedValue.Length > 0 ? cmbOldJobSiteSeq.SelectedItem.Text : "";
            Info.NewJobSiteName = cmbNewJobSiteSeq.SelectedValue.Length > 0 ? cmbNewJobSiteSeq.SelectedItem.Text : "";
            Info.OldFieldSeq = cmbOldFieldSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbOldFieldSeq.SelectedValue) : (int?)null;
            Info.NewFieldSeq = cmbNewFieldSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbNewFieldSeq.SelectedValue) : (int?)null;
            Info.OldExecSeq = cmbOldExecSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbOldExecSeq.SelectedValue) : (int?)null;
            Info.NewExecSeq = cmbNewExecSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbNewExecSeq.SelectedValue) : (int?)null;
            Info.OldPosTypeSeq = cmbOldPosType.SelectedValue.Length > 0 ? Int32.Parse(cmbOldPosType.SelectedValue) : (int?)null;
            Info.NewPosTypeSeq = cmbNewPosType.SelectedValue.Length > 0 ? Int32.Parse(cmbNewPosType.SelectedValue) : (int?)null;
            Info.OldClassSpecSeq = cmbOldClassSpec.SelectedValue.Length > 0 ? Int32.Parse(cmbOldClassSpec.SelectedValue) : (int?)null;
            Info.NewClassSpecSeq = cmbNewClassSpec.SelectedValue.Length > 0 ? Int32.Parse(cmbNewClassSpec.SelectedValue) : (int?)null;
            Info.OldSalaryQuota = txtOldSalary.Text.Length > 0 ? double.Parse(txtOldSalary.Text) : (double?)null;
            Info.NewSalaryQuota = txtNewSalary.Text.Length > 0 ? double.Parse(txtNewSalary.Text) : (double?)null;
            Info.StartDate = txtDateStart.GetStringValue().Length > 0 ? DateTime.Parse(txtDateStart.GetStringValue()) : (DateTime?)null;
            Info.Remarks = txtRemarks.Text.Length > 0 ? txtRemarks.Text : "";
            Info.UpdatedBy = ssMng.EmpSeqI;

            CmdPosDtls.Update(Info);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        

        protected void cmbOldPosType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            FillDataToFieldPositions(cmbOldPosType.SelectedValue, cmbOldFieldSeq);
            FillDataToClassSpec(cmbOldPosType.SelectedValue, cmbOldClassSpec);
        }

        protected void cmbNewPosType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            FillDataToFieldPositions(cmbNewPosType.SelectedValue, cmbNewFieldSeq);
            FillDataToClassSpec(cmbNewPosType.SelectedValue, cmbNewClassSpec);
        }

        protected void cmbNewFieldSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            IList<FieldInfo> Info = fieldBLL.ListFields(cmbNewFieldSeq.SelectedValue);
            if (Info.Count > 0)
                txtNewPosName.Text = Info[0].LineName;
            else
                txtNewPosName.Text = "";
        }

        protected void cmbPosSeq_SelectedIndexChanged1(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (cmbPosSeq.SelectedValue.Length > 0)
            {
                IList<PositionInfo> posList = posBLL.ListPositionsBySequence(cmbPosSeq.SelectedValue);
                PositionInfo posInfo = new PositionInfo();
                posInfo = posList[0];

                txtOldPosRef.Text = posInfo.RefNo;
                txtNewPosRef.Text = posInfo.RefNo;
                txtOldPosName.Text = posInfo.Name;
                txtNewPosName.Text = posInfo.Name;
                txtOldSalary.Text = string.Format("{0:N1}", posInfo.SalaryQuota.ToString());
                txtNewSalary.Text = string.Format("{0:N1}", posInfo.SalaryQuota.ToString());

                cmbOldPosType.SelectedValue = posInfo.PositionType.Sequence.ToString();
                cmbNewPosType.SelectedValue = posInfo.PositionType.Sequence.ToString();

                FillDataToFieldPositions(cmbOldPosType.SelectedValue, cmbOldFieldSeq);
                FillDataToFieldPositions(cmbNewPosType.SelectedValue, cmbNewFieldSeq);

                FillDataToClassSpec(cmbOldPosType.SelectedValue, cmbOldClassSpec);
                FillDataToClassSpec(cmbNewPosType.SelectedValue, cmbNewClassSpec);

                //cmbOldFieldSeq.SelectedValue = posInfo.Field.Sequence.ToString();
                //cmbNewFieldSeq.SelectedValue = posInfo.Field.Sequence.ToString();
                cmbOldExecSeq.SelectedValue = posInfo.Executive.Sequence.ToString();
                cmbNewExecSeq.SelectedValue = posInfo.Executive.Sequence.ToString();

                if (posInfo.ClassSpecification != null)
                    cmbOldClassSpec.SelectedValue = posInfo.ClassSpecification.Sequence.ToString();
                if (posInfo.ClassSpecification != null)
                    cmbNewClassSpec.SelectedValue = posInfo.ClassSpecification.Sequence.ToString();

                cmbOldJobSiteSeq.SelectedValue = posInfo.JobSite.Sequence.ToString();
                cmbNewJobSiteSeq.SelectedValue = posInfo.JobSite.Sequence.ToString();

                if (posInfo.Employee != null)
                    lblPosStatus.Text = posInfo.Employee.Person.FullName;
            }
        }
    }
}